﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Lidgren.Network;
//using IMLibrary.WS;
//using IMLibrary.WS.Net;
//using IMLibrary.WS.Server;
using IMLibrary.Server;
using IMLibrary.WS;

namespace Ourmsg.Server
{
    /// <summary>
    /// 多点会议单元服务器
    /// </summary>
    public class MCUServer: WebSocketService
    {
        #region 构造函数
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tcpPort"></param>
        /// <param name="udpPort"></param>
        public MCUServer(int tcpPort, int udpPort)
        {
            config = new NetPeerConfiguration("MCUServer"); 
            config.Port = udpPort;
        }
        #endregion
        
        #region 属性
        /// <summary>
        /// 服务配置
        /// </summary>
        NetPeerConfiguration config;
        /// <summary>
        /// 服务器节点
        /// </summary>
        NetPeer peer;
        /// <summary>
        /// 线程
        /// </summary>
        private Thread thUdpServer = null;
        /// <summary>
        /// 
        /// </summary>
        bool isStart = false;

        #endregion

        #region 服务
        /// <summary>
        /// 开始服务
        /// </summary>
        public void Start()
        {
            if (isStart) return;
            isStart = true;
            thUdpServer = new Thread(StartServer);
            thUdpServer.Start();
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        public void Stop()
        { 
            isStart = false;
            if (peer != null)
                peer.Shutdown("shutdown");

            if (thUdpServer != null)
                thUdpServer.Abort();
        }
        #endregion

        #region  开始服务
        private void StartServer()
        {
            peer = new NetPeer(config);
            peer.Start();
            while (isStart)
            {
                NetIncomingMessage msg;
                while ((msg = peer.ReadMessage()) != null)
                {
                    try
                    {
                        switch (msg.MessageType)
                        {
                            case NetIncomingMessageType.Data:

                                break;
                            case NetIncomingMessageType.UnconnectedData:
                              
                                break; 
                            case NetIncomingMessageType.DebugMessage:
                            case NetIncomingMessageType.VerboseDebugMessage:
                            case NetIncomingMessageType.WarningMessage:
                            case NetIncomingMessageType.ErrorMessage:
                                // print diagnostics message
                                Console.WriteLine(msg.ReadString());
                                break;
                        }
                    }
                    catch { }
                }
                System.Threading.Thread.Sleep(1);
            }
        }
        #endregion

        public override void OnClose(WebSocketSession session, CloseEventArgs e)
        {
             

        }

        public override void OnMessage(WebSocketSession session, MessageEventArgs e)
        {
           

        }

        public override void OnOpen(WebSocketSession session)
        {


        }
    }
}
